贪心算法之用优先队列(priority_queue)实现哈夫曼编码问题

1、问题

参考我的博客:贪心算法之哈夫曼编码问题

 

 

2、优先队列知识复习

参考我的博客: C++之STL之priority_queue

 

 

3、代码实现

#include <iostream>
#include <queue>

using namespace std;

//最大字符编码数组长度
#define MAXCODELEN 100
//最大哈夫曼节点结构体数组个数
#define MAXHAFF 100
//最大哈夫曼编码结构体数组的个数
#define MAXCODE 100
#define MAXWEIGHT  10000;


typedef struct Haffman
{
    //权重
    int weight;
    //字符
    char ch;
    //父节点
    int parent;
    //左儿子节点
    int leftChild;
    //右儿子节点
    int rightChild;
	//下标
	int index;
}HaffmaNode;

struct cmp 
{
	bool operator() (HaffmaNode haffman1, HaffmaNode haffman2)
	{
		return haffman1.weight > haffman2.weight;
	}	
};

typedef struct Code
{
    //字符的哈夫曼编码的存储
    int code[MAXCODELEN];
    //从哪个位置开始
    int sta
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是C++实现哈夫曼编码贪心算法的示例代码: ```c++ #include <iostream> #include <queue> #include <vector> #include <string> #include <unordered_map> using namespace std; // 定义哈夫曼树节点 struct HuffmanNode { char ch; // 字符 int freq; // 频率 HuffmanNode* left; // 左子节点 HuffmanNode* right; // 右子节点 HuffmanNode(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {} }; // 定义比较函数,用于优先队列 struct cmp { bool operator()(HuffmanNode* a, HuffmanNode* b) { return a->freq > b->freq; } }; // 构建哈夫曼树 HuffmanNode* buildHuffmanTree(unordered_map<char, int>& freqMap) { priority_queue<HuffmanNode*, vector<HuffmanNode*>, cmp> pq; for (auto& p : freqMap) { pq.push(new HuffmanNode(p.first, p.second)); } while (pq.size() > 1) { HuffmanNode* left = pq.top(); pq.pop(); HuffmanNode* right = pq.top(); pq.pop(); HuffmanNode* parent = new HuffmanNode('$', left->freq + right->freq); parent->left = left; parent->right = right; pq.push(parent); } return pq.top(); } // 递归遍历哈夫曼树,生成编码表 void generateHuffmanCode(HuffmanNode* root, string code, unordered_map<char, string>& codeMap) { if (!root) { return; } if (root->ch != '$') { codeMap[root->ch] = code; } generateHuffmanCode(root->left, code + "0", codeMap); generateHuffmanCode(root->right, code + "1", codeMap); } // 哈夫曼编码 string huffmanEncode(string s) { unordered_map<char, int> freqMap; for (char c : s) { freqMap[c]++; } HuffmanNode* root = buildHuffmanTree(freqMap); unordered_map<char, string> codeMap; generateHuffmanCode(root, "", codeMap); string res = ""; for (char c : s) { res += codeMap[c]; } return res; } // 哈夫曼解码 string huffmanDecode(string s, HuffmanNode* root) { string res = ""; HuffmanNode* cur = root; for (char c : s) { if (c == '0') { cur = cur->left; } else { cur = cur->right; } if (cur->ch != '$') { res += cur->ch; cur = root; } } return res; } // 测试代码 int main() { string s = "abacabad"; string encoded = huffmanEncode(s); cout << "Encoded string: " << encoded << endl; HuffmanNode* root = buildHuffmanTree({{'a', 2}, {'b', 2}, {'c', 1}, {'d', 1}}); string decoded = huffmanDecode(encoded, root); cout << "Decoded string: " << decoded << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码莎拉蒂 .

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值